<template>
	<view class="content">
		<view class="header" @tap="selectImg">
			<image src="/static/ic_add_identity@3x.png"></image>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {

			};
		},
		methods: {
			selectImg() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					success: (res) => {
						console.log(res);
						this.urlTobase64(res.tempFilePaths[0])
					}
				});
			},
			//图片转base64
			urlTobase64(url) {
				// #ifdef MP-WEIXIN
				uni.getFileSystemManager().readFile({
					filePath: url, //选择图片返回的相对路径
					encoding: 'base64', //编码格式
					success: res => { //成功的回调
						console.log(res);
						let base64 = 'data:image/jpeg;base64,' + res.data //不加上这串字符，在页面无法显示的哦
						this.$emit('base64', {
							base64: res.data,
							imgUrl: base64,
							url: url
						})
					},
					fail: (e) => {
						console.log("图片转换失败");
					}
				})
				// #endif

				// #ifndef MP-WEIXIN
				uni.request({
					url: url,
					method: 'GET',
					responseType: 'arraybuffer',
					success: ress => {
						let base641 = wx.arrayBufferToBase64(ress.data); //把arraybuffer转成base64 
						let base64 = 'data:image/jpeg;base64,' + base641 //不加上这串字符，在页面无法显示的哦
						this.$emit('base64', {
							base64: base641,
							imgUrl: base64,
							url: url
						})
					},
					fail: (e) => {
						console.log("图片转换失败");
					}
				})
				// #endif

			}
		}
	}
</script>

<style lang="scss">
	.content {
		height: 180rpx;
		width: 750rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		.header {
			width: 120rpx;
			height: 120rpx;
			background: #F5F7FA;
			border-radius: 8rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 40rpx;
				height: 40rpx;
			}
		}
	}
</style>
